package com.hot100.easy;

/**
 * @ClassName Demo23
 * @Description 合并二叉树
 * @Author zt
 * @Date 15:16 2022/6/16
 * @Version 1.0
 **/
public class Demo23 {



    public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }
  public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
      TreeNode newNode = new TreeNode();
        if(root1==null&&root2==null){
            return null;
        }
        if(root1==null){
            newNode.val=root2.val;
            newNode.left=mergeTrees(null,root2.left);
            newNode.right=mergeTrees(null,root2.right);
            return newNode;
        }
        if(root2==null){
            newNode.val=root1.val;
            newNode.left=mergeTrees(root1.left,null);
            newNode.right=mergeTrees(root1.right,null);
            return newNode;
        }
        newNode.val=root1.val+root2.val;
        newNode.left=mergeTrees(root1.left,root2.left);
        newNode.right=mergeTrees(root1.right,root2.right);
        return newNode;


    }
}